package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

import java.io.File;

/**
 * @description:
 * @author: Hupengpeng
 * @time: 2021/7/5 8:32
 */
public class VideoPartUploadInit {
    private String open_id;
    private String access_token;
    private String response;

    private VideoPartUploadInit() {
    }

    protected static VideoPartUploadInit New() {
        return new VideoPartUploadInit();
    }

    public VideoPartUploadInit execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_VIDEO_UPLOAD_PART_INIT,
                getOpen_id(),
                getAccess_token());
        String rs = HttpUtil.post(url, "");
        response = rs;
        return this;
    }
    public VideoPartUploadInitResponse convert() {
        try {
            JSONObject obj = JSONUtil.parseObj(response);
            JSONObject data = obj.getJSONObject("data");
            if (obj.getJSONObject("extra").getInt("error_code") == 0) {
                return JSONUtil.toBean(data, VideoPartUploadInitResponse.class);
            }
        } catch (Exception e) {
            throw new DouYinException(StrUtil.format("{}:{}", e.getMessage(), response));
        }
        throw new DouYinException(response);
    }

    public String getOpen_id() {
        return open_id;
    }

    public VideoPartUploadInit setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public VideoPartUploadInit setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public VideoPartUploadInit setResponse(String response) {
        this.response = response;
        return this;
    }
    public class VideoPartUploadInitResponse{
        private String description;
        private int error_code;
        private String upload_id;

        public String getDescription() {
            return description;
        }

        public VideoPartUploadInitResponse setDescription(String description) {
            this.description = description;
            return this;
        }

        public int getError_code() {
            return error_code;
        }

        public VideoPartUploadInitResponse setError_code(int error_code) {
            this.error_code = error_code;
            return this;
        }

        public String getUpload_id() {
            return upload_id;
        }

        public VideoPartUploadInitResponse setUpload_id(String upload_id) {
            this.upload_id = upload_id;
            return this;
        }
    }
}
